<?php

namespace app\admin\controller;

use app\admin\common\Common;
use app\admin\model\KmModel;
use app\admin\validate\KmValidate;
use think\Db;
use think\exception\DbException;
use think\Request;

class Km extends LoginThere
{
    public function index(){
        try {
            $listKm = Db::name('km')
                ->alias('k')
                ->join('app a', 'a.appid = k.appid')
                ->field('k.*,a.appname')
                ->paginate(10);
        } catch (DbException $e) {
            return Common::result(400,$e);
        }
        $page = $listKm->render();
        return $this->fetch('/km/index', ['list' => $listKm, 'page' => $page]);
    }

    public function delkm(Request $request){
        $data = $request->post();
        return KmModel::delkm($data);
    }

    public function addkm(Request $request){
        if ($request->isPost()){
            $data = $request->post();
            $validate = new KmValidate();
            if (!$validate->scene('addkm')->check($data)) {
                return Common::result(400,$validate->getError());
            }
            return KmModel::addkm($data);
        }else{
            return $this->fetch();
        }
    }

    public function searchkm(Request $request){
        if (!empty($data['km'])){
            return Common::result(400,"请输入完整");
        }
        $data = $request->post();
        return KmModel::searchkm($data);
    }

}